package com.snail.auth.security;

import com.alibaba.fastjson.JSON;
import com.common.ResultCodeEnum;
import com.common.ResultUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @autor houyanfeng 2021/3/5
 * @description: 用户没有登录时返回给前端的数据
 */

@Component
public class AjaxAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(ResultUtil.result(ResultCodeEnum.LOGIN_NEED_AUTHORITIES)));
        out.flush();
        out.close();
    }
}
